package org.jeecg.modules.flow.service.impl;

import org.jeecg.modules.flow.entity.Flow;
import org.jeecg.modules.flow.entity.FlowDetail;
import org.jeecg.modules.flow.entity.FlowVo;
import org.jeecg.modules.flow.mapper.FlowDetailMapper;
import org.jeecg.modules.flow.mapper.FlowMapper;
import org.jeecg.modules.flow.service.IFlowService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 流程表
 * @Author: jeecg-boot
 * @Date:   2020-07-24
 * @Version: V1.0
 */
@Service
public class FlowServiceImpl extends ServiceImpl<FlowMapper, Flow> implements IFlowService {

	@Autowired
	private FlowMapper flowMapper;
	@Autowired
	private FlowDetailMapper flowDetailMapper;

	@Override
	@Transactional
	public void saveMain(Flow flow, List<FlowDetail> flowDetailList) {
		flowMapper.insert(flow);
		List<FlowDetail> flowDetail = createFlowDetail(flow.getId(), flowDetailList);
		if (flowDetail != null && flowDetail.size()>0){
			flowDetailMapper.batchInsert(flowDetailList);
		}
	}

	private List<FlowDetail> createFlowDetail(String flowId,List<FlowDetail> flowDetailList){
		List<FlowDetail> updateFlowDet = new ArrayList<>();
		if(flowDetailList!=null && flowDetailList.size()>0) {
			int sort = 0;
			for(FlowDetail entity:flowDetailList) {
				sort = sort + 1;
				entity.setSort(sort);
				//外键设置
				entity.setFlowId(flowId);
				if (sort ==1 && flowDetailList.size()>1){
					entity.setNextProcId(flowDetailList.get(1).getProcId());
					entity.setNextProcName(flowDetailList.get(1).getProcName());
				}else if (sort==flowDetailList.size() && flowDetailList.size()>1){
					entity.setBeforeProcId(flowDetailList.get(flowDetailList.size()-2).getProcId());
					entity.setBeforeProcName(flowDetailList.get(flowDetailList.size()-2).getProcName());
				}else if (sort>0 && sort<flowDetailList.size() && flowDetailList.size()>1){
					entity.setNextProcId(flowDetailList.get(sort).getProcId());
					entity.setNextProcName(flowDetailList.get(sort).getProcName());
					entity.setBeforeProcId(flowDetailList.get(sort-2).getProcId());
					entity.setBeforeProcName(flowDetailList.get(sort-2).getProcName());
				}
				updateFlowDet.add(entity);
			}
		}
		return updateFlowDet;
	}

	@Override
	@Transactional
	public void updateMain(Flow flow,List<FlowDetail> flowDetailList) {
		flowMapper.updateById(flow);
		//1.先删除子表数据
		flowDetailMapper.deleteByMainId(flow.getId());

		List<FlowDetail> flowDetail = createFlowDetail(flow.getId(), flowDetailList);
		if (flowDetail != null && flowDetail.size()>0){
			flowDetailMapper.batchInsert(flowDetailList);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		flowDetailMapper.deleteByMainId(id);
		flowMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			flowDetailMapper.deleteByMainId(id.toString());
			flowMapper.deleteById(id);
		}
	}

	@Override
	public List<FlowVo> selectAll() {
		return baseMapper.selectAll();
	}

}
